;;  Copyright (c) Jeffrey Straszheim. All rights reserved.  The use and
;;  distribution terms for this software are covered by the Eclipse Public
;;  License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
;;  be found in the file epl-v10.html at the root of this distribution.  By
;;  using this software in any fashion, you are agreeing to be bound by the
;;  terms of this license.  You must not remove this notice, or any other,
;;  from this software.
;;
;;  test-eval.clj
;;
;;  A Clojure implementation of Datalog -- Eval Tests
;;
;;  straszheimjeffrey (gmail)
;;  Created 12 Feburary 2009


(ns clojure.datalog.tests.test-eval
  (:use clojure.contrib.test-is
	clojure.datalog.eval
        clojure.datalog.rules))


(def test-data
     {
      :users
      #{
        {:name "Fred"   :salary 30000 :super "Fred"}
        {:name "Mary"   :salary 20000 :super "Fred"}
        {:name "Becky"  :salary 10000 :super "Mary"}
        {:name "Sameer" :salary 13000 :super "Mary"}
        {:name "John"   :salary 12000 :super "Sameer"}
        }

      :transporation-type
      #{
        {:name "Luxury-Car" :position "Boss"}
        {:name "Car" :position "Manager"}
        }

      :position
      #{
        {:name "Boss" :min-salary 30000}
        {:name "Manager" :min-salary 20000}
        }
      })
      

(def test-rules
     [
      (logic-rule (:bosses :under ?x :over ?y) - (:users :name ?x :super ?y))
      (logic-rule (:bosses :under ?x :over ?y) - (:bosses :under ?x :over ?z)
                                                 (:bosses :under ?z :over ?y))

      (logic-rule (:holds :name ?x :position ?y) - (:users :name ?x :salary ?s)
                                                   (:position :name ?y :min-salary ?ms)
                                                   (if >= ?s ?ms))

      (logic-rule (:drives :name ?x :car ?y) - (:holds :name ?x :position ?p)
                                               (:transporation-type :name ?y :position ?p))

      ])

(def work-set (construct-prioritized-work-set test-rules))
(def results (run-work-elements work-set test-data))

(deftest test-eval
  (is (= (:bosses results)
         #{{:over "Sameer", :under "John"}
           {:over "Fred", :under "Sameer"}
           {:over "Fred", :under "Becky"}
           {:over "Fred", :under "Fred"}
           {:over "Fred", :under "John"}
           {:over "Mary", :under "Sameer"}
           {:over "Mary", :under "Becky"}
           {:over "Fred", :under "Mary"}
           {:over "Mary", :under "John"}}))

  (is (= (:holds results)
         #{{:name "Mary", :position "Manager"}
           {:name "Fred", :position "Boss"}
           {:name "Fred", :position "Manager"}}))

  (is (= (:drives results)
         #{{:name "Fred", :car "Car"}
           {:name "Mary", :car "Car"}
           {:name "Fred", :car "Luxury-Car"}})))

(comment
  (run-tests)
)

;; End of file
